package com.spic.business.hidden.type.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.hidden.type.domain.HiddenType;
import com.spic.business.hidden.type.domain.vo.HiddenTypeTree;
import com.spic.business.hidden.type.service.IHiddenTypeService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 隐患类型Controller
 *
 * @author qishaoping
 * @date 2021-10-17
 */
@RestController
@RequestMapping("/hidden/type")
public class HiddenTypeController extends BaseController
{
    @Autowired
    private IHiddenTypeService hiddenTypeService;

    /**
     * 查询隐患类型列表
     */
    @PreAuthorize(hasPermi = "business/hidden:type:list")
    @GetMapping("/list")
    public AjaxResult list(HiddenType hiddenType)
    {
        List<HiddenType> list = hiddenTypeService.selectHiddenTypeList(hiddenType);
        return AjaxResult.success(list);
    }

    /**
     * 导出隐患类型列表
     */
    @PreAuthorize(hasPermi = "business/hidden:type:export")
    @Log(title = "隐患类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiddenType hiddenType) throws IOException
    {
        List<HiddenType> list = hiddenTypeService.selectHiddenTypeList(hiddenType);
        ExcelUtil<HiddenType> util = new BusinessExcelUtil<HiddenType>(HiddenType.class);
        util.exportExcel(response, list, "隐患类型数据");
    }

    /**
     * 获取隐患类型详细信息
     */
    @PreAuthorize(hasPermi = "business/hidden:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(hiddenTypeService.selectHiddenTypeById(id));
    }

    /**
     * 新增隐患类型
     */
    @PreAuthorize(hasPermi = "business/hidden:type:add")
    @Log(title = "隐患类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiddenType hiddenType)
    {
        return toAjax(hiddenTypeService.insertHiddenType(hiddenType));
    }

    /**
     * 修改隐患类型
     */
    @PreAuthorize(hasPermi = "business/hidden:type:edit")
    @Log(title = "隐患类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult
    edit(@RequestBody HiddenType hiddenType)
    {
        return toAjax(hiddenTypeService.updateHiddenType(hiddenType));
    }

    /**
     * 删除隐患类型
     */
    @PreAuthorize(hasPermi = "business/hidden:type:remove")
    @Log(title = "隐患类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(hiddenTypeService.deleteHiddenTypeByIds(ids));
    }
    /**
     * 根据id查询隐患地点数据
     * @param hiddenType
     * @return
     */
    @GetMapping("/gethiddetype")
    public AjaxResult getbyHiddeType(HiddenType hiddenType)
    {
        List<HiddenType> hiddenTypeList = hiddenTypeService.hiddenTypeSelect(hiddenType);
        return AjaxResult.success(hiddenTypeList);
    }
    /**
     * 根据一级传的id并把它当作父Id查询二级数据
     * @param parentId
     * @return
     */
    @GetMapping("/getByParentid/{parentId}")
    public AjaxResult secondaryHiddenDangerByParentid(@PathVariable("parentId") String parentId)
    {
        List<HiddenType> hiddenTypeList =hiddenTypeService.seconddaryHidden(parentId);
        return AjaxResult.success(hiddenTypeList);
    }

    /**
     * 二级联动数据加载 以及页面数据解析
     * @param hiddenType
     * @return
     */
    @GetMapping("/getHiddeTypeByParentId")
    public AjaxResult getHiddeTypeByParentId(HiddenType hiddenType)
    {
        List<HiddenType> list = hiddenTypeService.selectHiddenTypeList(hiddenType);
        return AjaxResult.success(list);
    }



    @GetMapping("/hiddeTypeTree")
    public AjaxResult placetreeselect(HiddenType hiddenType)
    {
        List<HiddenType> list = hiddenTypeService.selectHiddenTypeList(hiddenType);
        List<HiddenTypeTree> hiddenTypes = hiddenTypeService.buildTreeSelect(list);
        return AjaxResult.success(hiddenTypes);
    }



}
